﻿using SD.Infrastructure.Repository.EntityFramework;
using SD.OpcProxy.Domain.Entities;
using SD.OpcProxy.Domain.IRepositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace SD.OpcProxy.Repository.Implements
{
    /// <summary>
    /// 变量历史仓储实现
    /// </summary>
    public class VariableHistoryRepository : EFAggRootRepositoryProvider<VariableHistory>, IVariableHistoryRepository
    {
        #region # 分页获取变量历史列表 —— ICollection<VariableHistory> FindByPage(string keywords...
        /// <summary>
        /// 分页获取变量历史列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="variableNo">变量编号</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="rowCount">总记录条数</param>
        /// <param name="pageCount">总页数</param>
        /// <returns>变量历史列表</returns>
        public ICollection<VariableHistory> FindByPage(string keywords, string variableNo, DateTime? startTime, DateTime? endTime, int pageIndex, int pageSize, out int rowCount, out int pageCount)
        {
            Expression<Func<VariableHistory, bool>> condition =
                x =>
                    (string.IsNullOrEmpty(keywords) || x.Keywords.Contains(keywords)) &&
                    (string.IsNullOrEmpty(variableNo) || x.VariableNo == variableNo) &&
                    (startTime == null || x.AddedTime >= startTime) &&
                    (endTime == null || x.AddedTime <= endTime);

            return base.FindByPage(condition, pageIndex, pageSize, out rowCount, out pageCount).ToList();
        }
        #endregion
    }
}
